package com.sanil.sample.ddb.controller;


import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
import com.sanil.sample.ddb.service.IDbInfoService;
import com.sanil.sample.ddb.vo.Result;
import com.snail.common.dyd.DataBaseOperator;
import com.snail.common.dyd.DynamicDataBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dds/dbInfo/")
@Slf4j
public class DynamicDataBaseController implements DynamicDataBase {


    @Autowired
    private IDbInfoService dbInfoService;
    @GetMapping("getDbNameList")
    public Result getDbNameList(String dsName) {
        log.info("getDbNameList:{}", dsName);
        Result<List<String>> result = new Result<>();
        try {
            List<String> allDataBaseName = dbInfoService.getAllDbName(dsName);
            result.setSuccess(true);
            result.setResult(allDataBaseName);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
            result.setMessage(String.format("查询失败：%s", e.getLocalizedMessage()));
        }
        return result;
    }

    @Override
    public void setDataBaseOperator(DataBaseOperator dataBaseOperator) {
        dbInfoService.setDataBaseOperator(dataBaseOperator);
    }

}
